/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A17
* ---------

* Globals
global dep "risk tax_rate valitrade_advice nup_gap_i_int htrl_i_int"

* Matrix
mat tableA17 = J(6,5,.)
mat tableA17_T = J(1,5,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' bae_s if sample_reg==1 , absorb(ms ym_*) vce(cluster key_importer key_dec)

	mat tableA17[`i',`j'] = _b[bae_s]
	local i = `i' + 1
	mat tableA17[`i',`j'] = _se[bae_s]
	mat tableA17_T[1,`j'] = e(N)
	local i = `i' + 4
	mat tableA17[`i',`j'] = e(r2)
	local i = `i' -5
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A17.xlsx", replace sheet(TableA17) // create a new excel spreadsheet
putexcel B4 = matrix(tableA17), nformat(0.000) hcenter vcenter
putexcel B8 = matrix(tableA17_T), nformat(number_sep) hcenter vcenter

* Title
putexcel (A1:F1), merge hcenter vcenter
putexcel A1 = "Before delegated randomization of inspector assignment"

* Column names
putexcel A2 = "Dependent variable", left vcenter bold
local cells `" "B" "C" "D" "E" "F" "'
local labels `" "Risk score" "Tax rate" "Valuation advice dummy" "Log initial unit price" "Initial hyp. tax rev. losses" "'
forvalues i = 1(1)5 {

	local x: word `i' of `labels'
	local y: word `i' of `cells'

	putexcel `y'2 = "`x'", hcenter vcenter bold
	putexcel `y'3 = "(`i')", hcenter vcenter

	forvalues j = 6(1)7 {
		putexcel `y'`j' = "Yes", hcenter vcenter
	}
}

* Row names
putexcel A4 = "Brokers' excess interaction (that semester)", left vcenter
putexcel A6 = "Importer-semester fixed effects", left vcenter
putexcel A7 = "Month-year fixed effects", left vcenter
putexcel A8 = "Observations", left vcenter
putexcel A9 = "R-squared", left vcenter


* -------------------------------- ENDS HERE -------------------------------- *